@extends('layouts.master')

@section('title')
    Canchas
@stop

@section('content')
<div class="row">
	<div class="col-lg-12">
		<p><a class="btn btn-default" href="{{ URL::to('fields/create') }}"><i class="fa fa-plus-square"></i> Nueva cancha</a></p>
		<section class="panel">
            <div class="table-responsive">
				<table class="table table-striped m-b-none" data-ride="datatables">
					<thead>
						<tr>
		                	<th colspan="10">
			                   	<div class="row">                        
		    			            <div class="col-sm-8">
		                				<div class="select filter m-b-small m-t-small" data-resize="auto">
		                					<label class="col-lg-6 control-label" style="padding-left: 0px; width: 100px; padding-top: 7px;" for="matchTypeSelector">Cargada por usuario  </label>
		                					<select id="uploadedByUserSelector" name="uploadedByUserSelector" style="width: 129px;">
									        	<option></option>
									            <option value='0'>No</option>
									            <option value='1'>Sí</option>
									    	</select>
			                           	</div>
		                        	</div>
		                      	</div>
		                    </th>
		                </tr>
						<tr>
							<th>ID</th>
							<th>Nombre</th>
							<th>Cargada por usuario</th>
							<th>Dirección</th>
							<th>Localidad</th>
							<th>Provincia</th>
							<th>Partidos organizados</th>
							<th>Partidos jugados</th>
							<th>Actions</th>
						</tr>
					</thead>
					<tbody>
					<!-- display stored fields -->
					@foreach($fields as $key => $value)
						<tr>
							<td>{{ $value->id }}</td>
							<td>{{ $value->name }}</td>
							<td class="cancelados"><input style="height: auto; width: auto;" type="checkbox" disabled="true" <?php if($value->uploaded_by_user) echo "checked='checked'" ?> /></td>
							<td>{{ $value->address }}</td>
							<td>{{ $value->town }}</td>
							<td>{{ $value->state }}</td>
							<td>{{ $value->totalMatches }}</td>
							<td>{{ $value->playedMatches }}</td>
				
							<!-- we will also add show, edit, and delete buttons -->
							<td>
								<a class="btn btn-small btn-success" href="{{ URL::to('fields/' . $value->id) }}">Ver datos</a>
								<a class="btn btn-small btn-info" href="{{ URL::to('fields/' . $value->id . '/edit') }}">Editar</a>
								
								{{ Form::button('Eliminar', array(
				                	'onclick' =>    'showConfirmDeleteModal(
				                    	"' . $value->name . '",
				                        "' . url('fields/' . $value->id) . '"
				                	)',
				                    'class' => 'btn btn-danger',
				                 )) }}
							</td>
						</tr>
					@endforeach
					</tbody>
				</table>
            </div>
    	</section>
	</div>
</div>
@stop

@section('modals')
 {{-- Modal: Confirm delete --}}
    <div class="modal fade" id="modal-confirmDelete" tabindex="-1" role="dialog" aria-labelledby="confirmDelete" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="modal-title">Eliminar</h3>
                </div>
                <div class="modal-body">
                    <p>
                        <strong>Esta seguro que desea eliminar este elemento?</strong>
                    </p>
                    <p id="deletePageName" class="text-danger"></p>
                </div>
                <div class="modal-footer">
                    {{ Form::open(array('id' => 'deleteForm', 'method' => 'delete')) }}
                        {{ Form::button('Cancelar', array(
                            'data-dismiss' => 'modal',
                            'class' => 'btn btn-default',
                        )) }}
                        {{ Form::submit('Eliminar', array(
                            'class' => 'btn btn-default',
                        )) }}
                    {{ Form::close() }}
                </div>
            </div>{{-- /.modal-content --}}
        </div>{{-- /.modal-dialog --}}
    </div>{{-- /.modal --}}
@stop

@section('scripts')
<script>
	$.fn.dataTable.ext.search.push(
		function( settings, data, dataIndex ) {
			var uploadedByUserSelected = $('#uploadedByUserSelector').val();
			var uploadedByUser = data[2];
		 
			if(uploadedByUserSelected == "")
				return true;
			if (uploadedByUserSelected == "1") {
				if(uploadedByUser.indexOf("checked") != -1) 
					return true;
				else
					return false;
			} else {
				if(uploadedByUser.indexOf("checked") == -1) 
					return true;
				else
					return false;
			}
		}
	);

	function showConfirmDeleteModal(name, url) {
    	$('#deleteForm').prop('action', url);
        $('#deletePageName').text(name);

        $('#modal-confirmDelete').modal({
        	show: true
        });
	}

	$(document).ready(function() {
		// datatable
		var oTable = $('[data-ride="datatables"]').DataTable( {
			"sDom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col col-sm-6'p>>",
			"sPaginationType": "full_numbers",
			"language": {
	            "lengthMenu": "Mostrar _MENU_ registros por página",
	            "zeroRecords": "No se encontraron resultados",
	            "info": "Mostrando página _PAGE_ de _PAGES_",
	            "infoEmpty": "No hay datos para mostrar",
	            "infoFiltered": "(filtrado de un total de _MAX_ registros)"
	        },
	        "aoColumnDefs": [
		                       {
		                          bSortable: false,
		                          aTargets: [ -1 ]
		                       }
		                     ]
		} );
	
		$('#uploadedByUserSelector').change( function() {
			oTable.draw();
	    });
	});
</script>	
@stop